package com.shizhanzhe.qdd.home;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.qdd.R;
import com.shizhanzhe.qdd.base.UserBean;
import com.shizhanzhe.qdd.home.activity.ApprenticeDetailsActivity;
import com.shizhanzhe.qdd.home.activity.BindingPhoneActivity;
import com.shizhanzhe.qdd.home.activity.LoginActivity;
import com.shizhanzhe.qdd.home.activity.MyInfoActivity;
import com.shizhanzhe.qdd.home.activity.SettingActivity;
import com.shizhanzhe.qdd.home.activity.TiXianActivity;
import com.shizhanzhe.qdd.home.activity.TiXianRecordActivity;
import com.shizhanzhe.qdd.home.activity.TodayRecordActivity;
import com.shizhanzhe.qdd.home.activity.TotalRecordActivity;
import com.shizhanzhe.qdd.home.bean.InCome;
import com.shizhanzhe.qdd.url.Path;
import com.shizhanzhe.qdd.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.qdd.widget.CircleImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author YJ
 * @date 2019/1/3
 * 个人中心
 */
public class HomeFragment extends Fragment {
    Unbinder unbinder;
    @BindView(R.id.headimg)
    CircleImageView headimg;
    @BindView(R.id.nickname_tv)
    TextView nickname;
    @BindView(R.id.pid)
    TextView pid;
    @BindView(R.id.my_today)
    TextView myToday;
    @BindView(R.id.my_yue)
    TextView myYue;
    @BindView(R.id.my_total)
    TextView myTotal;
    @BindView(R.id.my_tudi)
    TextView myTudi;
    @BindView(R.id.my_shoutu)
    TextView myShoutu;
//    @BindView(R.id.my_tusun)
//    TextView myTusun;
    @BindView(R.id.my_yuedu)
    TextView myYuedu;
    @BindView(R.id.dangqianyuer)
    TextView dangqianyuer;
    @BindView(R.id.phone)
    TextView phone;
    private HomeListener mHomeListener;
    private InCome inCome;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);
        initImmersionBar();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    @SuppressLint("SetTextI18n")
    private void initData() {
        Glide.with(getContext()).load(UserBean.headimg).apply(new RequestOptions().error(R.mipmap.head)).into(headimg);
        if (UserBean.uid != 0) {
            pid.setVisibility(View.VISIBLE);
            nickname.setText(UserBean.nickname);
            String upid = UserBean.pid == 0 ? "暂无" : UserBean.pid + "";
            pid.setText(upid);
            income();
        } else {
            nickname.setText("暂未登录");
            pid.setVisibility(View.GONE);
        }
        if ("0".equals(UserBean.mobile)){
            phone.setText("未绑定");
        }else {
            phone.setText(UserBean.mobile.substring(0,3) + "****" + UserBean.mobile.substring(7,UserBean.mobile.length()));
        }
    }

    @OnClick({R.id.iv_myinfo_setting, R.id.ll_myinfo_editinfo, R.id.tixianarea, R.id.my_tixian, R.id.ll_today, R.id.ll_task_record,
            R.id.ll_ti_record, R.id.ll_effective, R.id.ll_Mentor, R.id.my_zhuanfa, R.id.my_shoutu_zhuanqian,R.id.bind})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //设置
            case R.id.iv_myinfo_setting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            //个人信息
            case R.id.ll_myinfo_editinfo:
                if (UserBean.uid !=0) {
                    startActivity(new Intent(getActivity(), MyInfoActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            //提现
            case R.id.tixianarea:
            case R.id.my_tixian:
                if (UserBean.uid !=0) {
                    startActivity(new Intent(getActivity(), TiXianActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            //今日收入
            case R.id.ll_today:
                startActivity(new Intent(getActivity(), TodayRecordActivity.class));
                break;
            //累计收入
            case R.id.ll_task_record:
                startActivity(new Intent(getActivity(), TotalRecordActivity.class));
                break;
            //提现记录
            case R.id.ll_ti_record:
                startActivity(new Intent(getActivity(), TiXianRecordActivity.class));
                break;
            //有效徒弟
            case R.id.ll_effective:
                startActivity(new Intent(getActivity(), ApprenticeDetailsActivity.class).putExtra("type", 4).putExtra("tdzs",inCome.getData().getTdzs()).putExtra("yxtd",inCome.getData().getYxtd()));
                break;
            case R.id.ll_Mentor:
                startActivity(new Intent(getActivity(), ApprenticeDetailsActivity.class).putExtra("type", 5).putExtra("tdzs",inCome.getData().getTdzs()).putExtra("yxtd",inCome.getData().getYxtd()));
                break;
            case R.id.my_zhuanfa:
                //跳转分享赚钱
                mHomeListener.homeclick(1);
                break;
            case R.id.my_shoutu_zhuanqian:
                //跳转收徒赚钱
                mHomeListener.homeclick(2);
                break;
            case R.id.bind:
                if (UserBean.uid !=0) {
                    startActivity(new Intent(getActivity(), BindingPhoneActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            default:
        }
    }

    private void income() {
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.INCOMELIST(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onsendJson(String json) {
                Log.d("______", json);
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        inCome = new Gson().fromJson(json, InCome.class);
                        myToday.setText("" + inCome.getData().getJrsr());
                        myYue.setText("" + inCome.getData().getRwsr());
                        myTotal.setText("" + inCome.getData().getLjtx());
                        myTudi.setText(inCome.getData().getYxtd()+"");
                        myShoutu.setText(inCome.getData().getTdzs()+"");
//                        myTusun.setText(inCome.getData().getTszs()+"");
                        myYuedu.setText(inCome.getData().getYdzs()+"");
                        dangqianyuer.setText(inCome.getData().getBalance());
                        UserBean.balance = inCome.getData().getBalance();
                        UserBean.servicewx = inCome.getData().getKefu();
                    } catch (Exception e) {
                        Toast.makeText(getContext(), "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getContext(), "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    protected ImmersionBar mImmersionBar;
    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
        mImmersionBar.statusBarDarkFont(true,0.2f)
                .init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    public void setHomeListener(HomeListener homeListener) {
        mHomeListener = homeListener;
    }

    public interface HomeListener {
        void homeclick(int homeinto);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
